package com.trade.wzj.common.yang;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trade.wzj.model.xiang.ProductInfoQuota;
import com.trade.wzj.model.xiang.SupplierInfo;
import com.trade.wzj.model.xiang.SupplierQuota;
import com.trade.wzj.service.yang.Supplier_quotaService;

@Controller
@RequestMapping("/Supplier_quotaController_Yang")
public class Supplier_quotaController_Yang {

	@Resource(name = "supplier_quota")
	private Supplier_quotaService Supplier_quotaService;

	public Supplier_quotaService getSupplier_quotaService() {
		return Supplier_quotaService;
	}

	public void setSupplier_quotaService(Supplier_quotaService supplier_quotaService) {
		Supplier_quotaService = supplier_quotaService;
	}

	/**
	 * 模糊查询所有供应商信息（包括报价、支付类型、供应商信息、商品信息）
	 * 
	 * @param name
	 *            前台传过来的供应商名称
	 * @param currentPages
	 * @return
	 */
	@RequestMapping("findSupplierQuota")
	@ResponseBody
	public PageInfo<SupplierQuota> findSupplierQuota(@RequestParam(value = "name", required = false) String name,
			@RequestParam(value = "currentPages", required = false) Integer currentPages) {
		// 起始页数
		Integer currentPage = 1;
		// 每条显示的行数
		int rowPerPage = 5;
		PageInfo<SupplierQuota> pageInfo = null;
		// 结果集列表
		List<SupplierQuota> list = null;
		try {
			if (null != currentPages) {
				// 页码不为空时，将当前页信息赋值
				currentPage = currentPages;
			}
			PageHelper.startPage(currentPage, rowPerPage);
			list = Supplier_quotaService.findSupplierQuota(name);

			pageInfo = new PageInfo<SupplierQuota>(list);

			System.out.println(list.size());
			System.out.println(pageInfo.getList());
			for (SupplierQuota supplierQuota : list) {
				System.out.println(supplierQuota);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}

	/**
	 * 按id查询报价单号中的信息（包括报价、支付类型、供应商信息、商品信息）
	 * 
	 * @param id
	 *            前台传过来的报价单号的主键
	 * @param currentPages
	 * @return
	 */
	@RequestMapping("findSupplierQuotaById")
	@ResponseBody
	public PageInfo<SupplierQuota> findSupplierQuotaById(@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "currentPages", required = false) Integer currentPages) {
		// 起始页数
		Integer currentPage = 1;
		// 每条显示的行数
		int rowPerPage = 5;
		PageInfo<SupplierQuota> pageInfo = null;
		// 结果集列表
		List<SupplierQuota> list = null;
		try {
			if (null != currentPages) {
				// 页码不为空时，将当前页信息赋值
				currentPage = currentPages;
			}
			PageHelper.startPage(currentPage, rowPerPage);
			list = Supplier_quotaService.findSupplierQuotaById(id);

			pageInfo = new PageInfo<SupplierQuota>(list);

			System.out.println(list.size());
			System.out.println(pageInfo.getList());
			for (SupplierQuota supplierQuota : list) {
				System.out.println(supplierQuota);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}

	/**
	 * 按商品名称模糊查询报价信息（包含商品和商家和付款类型查询）
	 * 
	 * @param productName
	 *            商品名称
	 * @param currentPages
	 * @return
	 */
	@RequestMapping("findSupplierQuotaByProductName")
	@ResponseBody
	public PageInfo<SupplierQuota> findSupplierQuotaByProductName(
			@RequestParam(value = "productName", required = false) String productName,
			@RequestParam(value = "currentPages", required = false) Integer currentPages) {
		// 起始页数
		Integer currentPage = 1;
		// 每条显示的行数
		int rowPerPage = 5;
		PageInfo<SupplierQuota> pageInfo = null;
		// 结果集列表
		List<SupplierQuota> list = null;
		try {
			if (null != currentPages) {
				// 页码不为空时，将当前页信息赋值
				currentPage = currentPages;
			}
			PageHelper.startPage(currentPage, rowPerPage);
			list = Supplier_quotaService.findSupplierQuotaByProductName(productName);

			pageInfo = new PageInfo<SupplierQuota>(list);

			System.out.println(list.size());
			System.out.println(pageInfo.getList());
			for (SupplierQuota supplierQuota : list) {
				System.out.println(supplierQuota);
				for (ProductInfoQuota productInfoQuota : supplierQuota.getProductInfoQuota()) {
					System.out.println("商品报价日期："+productInfoQuota.getExpirationDate());
				}
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}

	
	@RequestMapping("/getListProduct")
	@ResponseBody
	public List<SupplierQuota> getListProduct(int[] list){
		List<SupplierQuota> lis = null;
		try {
			
			lis=Supplier_quotaService.getListProduct(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lis;
	}
	
	
	
	@RequestMapping("getAllSupplierQuotas")
	@ResponseBody
	public PageInfo<SupplierQuota> getAllSupplierQuotas(
			@RequestParam(value = "supplierQuotaNo", required = false) String supplierQuotaNo,
			@RequestParam(value = "currentPages", required = false) Integer currentPages) {
		// 起始页数
		Integer currentPage = 1;
		// 每条显示的行数
		int rowPerPage = 5;
		PageInfo<SupplierQuota> pageInfo = null;
		// 结果集列表
		List<SupplierQuota> list = null;
		try {
			if (null != currentPages) {
				// 页码不为空时，将当前页信息赋值
				currentPage = currentPages;
			}
			PageHelper.startPage(currentPage, rowPerPage,true);
			list = Supplier_quotaService.getAllSupplierQuotaInfos(supplierQuotaNo);

			pageInfo = new PageInfo<SupplierQuota>(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageInfo;
	}
	
	//通过供应商id查询供应商报价产品信息
	@RequestMapping("/getAllSupplierProduct")
	@ResponseBody
	public List<ProductInfoQuota> getAllSupplierProduct(
			@RequestParam(value = "supplierInfoNo", required = false) Integer supplierInfoNo) {
		
		List<ProductInfoQuota> list = null;
		try {
			
			list = Supplier_quotaService.checkAllProductBySupplierInfo(supplierInfoNo);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//查询供应商基本信息及可按照id查询供应商信息
	@RequestMapping("/checkAllSupplierInfo")
	@ResponseBody
	public List<SupplierInfo> checkAllSupplierInfo(@RequestParam(value="supplierInfoId",required=false)Integer supplierInfoId){
		List<SupplierInfo> list=null;
		try {
			list=Supplier_quotaService.getSupplierInfoB(supplierInfoId);
			//list=Supplier_quotaService.checkAllSupplierInfos(supplierInfoId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	
	//添加供应商报价表信息
	@RequestMapping("/addSupplierPrice")
	@ResponseBody
	public boolean addSupplierPrice(@RequestParam("supplierSuppiers")Integer supplierSuppiers,
									@RequestParam("productName")String productName,
									@RequestParam("productNum")Integer productNum,
									@RequestParam("Unit")String Unit,
									@RequestParam("quotaDate")String quotaDate,
									@RequestParam("productDelivery")Integer productDelivery,
									@RequestParam("productPrice")Double productPrice,
									@RequestParam("rental")Double rental,
									@RequestParam("expirationDate")String expirationDate) {
		boolean bol=false;
		try {
			
			Integer result=Supplier_quotaService.getSupplierId(supplierSuppiers);
			ProductInfoQuota pc = new ProductInfoQuota();
			pc.setProductName(productName);
			pc.setUnit(Unit);
			pc.setProductDelivery(productDelivery);
			pc.setProductNum(productNum);
			
			pc.setProductPrice(productPrice);
			pc.setExpirationDates(expirationDate);
			pc.setRental(rental);
			if(null!=result) {//不为空，说明报价表中有当前供应商存在，可直接获取报价编号，直接添加产品表
				pc.setSupplierquotaID(result);
				Supplier_quotaService.addProducts(pc);
				//调用添加产品表方法
				
			}else {//为空，说明报价表中没有当前供应商存在，需先添加报价表
				//生成供应商报价单
				Integer ic = (int) (Math.random() * 9000) + 1000;
				//报价编号供应商
				String companyPurchase = String.valueOf(ic);
				SupplierQuota sq = new SupplierQuota();
				sq.setSupplierquotaID(companyPurchase);
				sq.setSupplierID(supplierSuppiers);
				sq.setQuotaDates(quotaDate);
				sq.setPayID(1);
				Supplier_quotaService.addSupplierPrice(sq);
				//获取报价变自增编号
				Integer quotaId=sq.getId();
				pc.setSupplierquotaID(quotaId);
				Supplier_quotaService.addProducts(pc);
			}
			bol=true;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return bol;
	}
	
	
	
	
	//查询供应商基本信息及可按照id查询供应商信息
		@RequestMapping("/checkAllSupplierIns")
		@ResponseBody
		public List<SupplierInfo> checkAllSupplierIns(@RequestParam(value="supplierInfoId",required=false)Integer supplierInfoId){
			List<SupplierInfo> list=null;
			try {
				//list=Supplier_quotaService.getSupplierInfoB(supplierInfoId);
				list=Supplier_quotaService.checkAllSupplierInfos(supplierInfoId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return list;
		}
	
	
	
	
	
}
